package org.freez4.spring.resource;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;

import java.io.IOException;
import java.io.Reader;
import java.util.logging.Logger;

/**
 * {@link FileSystemResourceLoader} 示例
 *
 * @author 阿坤同学
 * @date 2024/1/17 22:59
 */
public class EncodedFileSystemResourceLoaderDemo {

    public static final Logger logger = Logger.getLogger(EncodedFileSystemResourceLoaderDemo.class.getName());

    public static void main(String[] args) throws IOException {
        String currentJavaFilePath = "/" + System.getProperty("user.dir") + "/springframework/resource/src/main/java/org/freez4/spring/resource/EncodedFileSystemResourceLoaderDemo.java";
        // 新建一个 FileSystemResourceLoader 对象
        FileSystemResourceLoader resourceLoader = new FileSystemResourceLoader();
        // FileSystemResource => WritableResource => Resource
        Resource resource = resourceLoader.getResource(currentJavaFilePath);
        EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
        // 字符输入流
        try (Reader reader = encodedResource.getReader()) {
            String content = String.format("EncodedFileSystemResourceLoaderDemo.java 源码:%s", IOUtils.toString(reader));
            logger.info(content);
        }
    }
}